﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OnlineTestBLL;
using OnlineTestModel;
using FineUI;

namespace OnlineTestUI
{
    public partial class paperlist : System.Web.UI.Page
    {
        private PapersBLL pb = new PapersBLL();
        protected Students s = new Students();
        protected void Page_Load(object sender, EventArgs e)
        {

            if (Session["student"] != null)
            {
                s = (Students) Session["student"];
                if (!IsPostBack)
                {
                    BindGrid(s.Id);
                }
            }
            else
            {
                PageContext.Redirect("login.aspx");
            }


        }
        #region BindGrid
        private void BindGrid(int studentid)
        {
            // 1.设置总项数（特别注意：数据库分页一定要设置总记录数RecordCount）
            if (s.Status == 0)
            {
                GridPaper.RecordCount =
                    pb.GetRecordCount("id not in (select paperid from studentpapers where studentid =" + studentid +
                                      ") and isfinished = 1 and status='0'");
            }
            else
            {
                GridPaper.RecordCount =
                    pb.GetRecordCount("id not in (select paperid from studentpapers where studentid =" + studentid +
                                      ") and isfinished = 1 ");
            }
            // 2.获取当前分页数据
            DataTable table = GetPagedDataTable(studentid);

            GridPaper.DataSource = table;

            GridPaper.DataBind();
        }


        /// <summary>
        /// 数据库分页
        /// </summary>
        /// <returns></returns>
        private DataTable GetPagedDataTable(int studentid)
        {
            int pageIndex = GridPaper.PageIndex;
            int pageSize = GridPaper.PageSize;

            string sortField = GridPaper.SortField;
            string sortDirection = GridPaper.SortDirection;

            int rowbegin = pageIndex * pageSize+1;
            int rowend = (pageIndex + 1) * pageSize;
            string orderby = String.Format("{0} {1}", sortField, sortDirection);
            DataTable paperList = new DataTable();
            if (s.Status == 0)
            {
                paperList = pb.GetListByPage("id not in (select paperid from studentpapers where studentid =" + studentid + ")  and isfinished = 1 and status='0'", @orderby, rowbegin, rowend).Tables[0];
                
            }
            else
            {
                paperList = pb.GetListByPage("id not in (select paperid from studentpapers where studentid =" + studentid + ") and isfinished = 1 ", @orderby, rowbegin, rowend).Tables[0];
                
            }
            

            return paperList;
        }

        #endregion

        #region Events




        protected void GridPaper_PageIndexChange(object sender, FineUI.GridPageEventArgs e)
        {
            GridPaper.PageIndex = e.NewPageIndex;
            BindGrid(s.Id);
        }

        protected void GridPaper_Sort(object sender, FineUI.GridSortEventArgs e)
        {
            GridPaper.SortDirection = e.SortDirection;
            GridPaper.SortField = e.SortField;
            BindGrid(s.Id);
        }

        #endregion


    }
}